package com.cya.lotterysystem.service.impl;

import com.cya.lotterysystem.controller.param.CreatePrizeParam;
import com.cya.lotterysystem.controller.param.PageParam;
import com.cya.lotterysystem.dao.dataobject.PrizeDO;
import com.cya.lotterysystem.dao.mapper.PrizeMapper;
import com.cya.lotterysystem.service.PictureService;
import com.cya.lotterysystem.service.PrizeService;
import com.cya.lotterysystem.service.dto.PageListDTO;
import com.cya.lotterysystem.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/11/1 21:25
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class PrizeServiceImpl implements PrizeService {

    @Autowired
    private PictureService pictureService;

    @Autowired
    private PrizeMapper prizeMapper;

    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
//       上传图片
       String fileName=pictureService.savePicture(picFile);
//存库
        PrizeDO prizeDO=new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setImageUrl(fileName);
        prizeDO.setPrice(param.getPrice());

        prizeMapper.insert(prizeDO);
        return prizeDO.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
//            总量

        int total=prizeMapper.count();

//        查询当前页列表

        List<PrizeDTO> prizeDTOList=new ArrayList<>();

        List<PrizeDO> prizeDOList=prizeMapper.selectPrizeList(param.offset(),param.getPageSize());

        for (PrizeDO prizeDO: prizeDOList){

            PrizeDTO prizeDTO=new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());

            prizeDTOList.add(prizeDTO);

        }



        return new PageListDTO<>(total,prizeDTOList);
    }
}